package com.milo.thread.create.callable;

import java.util.concurrent.*;

/**
 * <p>
 *     本测试类主要演示Callable和Future相结合在多线程中获取结果值
 * </p>
 * @author milo
 * @date 2019/4/2015:42
 *
 */
public class CallableTest1 {

    public static void main(String[] args) {


        ExecutorService executor = Executors.newCachedThreadPool();
        //创建任务
        MyCallable task = new MyCallable();
        //提交任务
        Future<Integer> result = executor.submit(task);
        //有序关闭
        executor.shutdown();
        try {
            TimeUnit.MILLISECONDS.sleep(100);
        } catch (InterruptedException e1) {
            e1.printStackTrace();
        }

        System.out.println("主线程在执行任务");

        try {
            System.out.println("task运行结果" + result.get());
        } catch (InterruptedException | ExecutionException e) {
            e.printStackTrace();
        }

        System.out.println("所有任务执行完毕");
    }
}
